package cn.cvs.servlet.supplier;

import cn.cvs.service.SupplierService;
import cn.cvs.util.SpringUtil;
import com.alibaba.fastjson.JSON;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet(name = "SuppilerDelServlet", urlPatterns = "/sys/supplier/del")
public class SuppilerDelServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet ( request, response );
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding ( "utf-8" );
        response.setContentType ( "text/html;charset=UTF-8" );
        SupplierService supplierService = (SupplierService) SpringUtil.getBean ( "supplierService" );
        PrintWriter writer = response.getWriter ( );

        Integer id = Integer.valueOf ( request.getParameter ( "id" ) );
        int count = supplierService.getCountByQueryJoinEntity ( id );
        Map map=new HashMap ();
        if (count>0){
            map.put ( "delResult", count+"" );
        }else {
            List<Integer> ids=new ArrayList<> (  );
            ids.add ( id );
            int delete = supplierService.delete ( ids );
            if (delete>0){
                map.put ( "delResult","true" );
            }else {
                map.put ( "delResult","false" );
            }
        }
        writer.write ( JSON.toJSONString(map) );
        writer.close ();
    }
}
